package com.qzdsoft.erpcloud.controller.com;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.domain.com.CustomerSupplierContacts;
import com.qzdsoft.erpcloud.domain.com.Department;
import com.qzdsoft.erpcloud.domain.com.SupplierCustomer;
import com.qzdsoft.erpcloud.service.act.CenterCompanyService;
import com.qzdsoft.erpcloud.service.com.CustomerSupplierService;
import com.qzdsoft.erpcloud.service.com.DepartmentService;
import com.qzdsoft.erpcloud.service.com.StaffService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.vo.com.StaffInfo;
import com.qzdsoft.erpcloud.vo.com.SupplierCustomerQueryInfo;
import com.qzdsoft.erpcloud.vo.com.SupplierCustomerQueryParam;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.com.SupplierCustomerCatagoryType;
import com.qzdsoft.vo.pu.SupplierCustomerType;

/**
 * 客户管理
 * @author pc-20170422
 *
 */
@Controller
@RequestMapping("/com/supplier/customer")
public class SupplierCustomerController {
    private static final String  BASEPATH = "com/supplierCustomer";
    private static final Logger logger = LoggerFactory.getLogger(SupplierCustomerController.class);
	@Autowired
	private CustomerSupplierService customerSupplierService;
	@Autowired
	private CenterCompanyService actCenterCompanyService;
	@Autowired
	private DepartmentService departmentService;
	@Autowired
	private StaffService staffService;
	
	@Permission(validate=false)
	@ResponseBody
	@RequestMapping("/contacts/{supplierCustomerId}")
	public List<CustomerSupplierContacts> contacts(@PathVariable("supplierCustomerId")String supplierCustomerId){
		return customerSupplierService.selectAll(supplierCustomerId);
	}
 
	@Permission(validate=false)
    @ResponseBody
    @RequestMapping("/supplierList")
    public LayTableResponse<SupplierCustomer> selectSuppliers(SupplierCustomerQueryInfo info)
    {
        logger.debug("查询供应商:params={}",info);
        info.setType(SupplierCustomerType.SUPPLIER_CODE);
        List<SupplierCustomer> supplierCustomers = customerSupplierService.selectBy(info);
        return new LayTableResponse<SupplierCustomer>(supplierCustomers);
    }
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/customerList")
    public LayTableResponse<SupplierCustomer> selectCustomer(SupplierCustomerQueryInfo info)
    {
        logger.debug("查询客户:params={}",info);
        info.setType(SupplierCustomerType.CUSTOMER_CODE);
        List<SupplierCustomer> supplierCustomers = customerSupplierService.selectBy(info);
        return new LayTableResponse<SupplierCustomer>(supplierCustomers);
    }
	
    //  1供应商 2客户
	  @Permission(validate=false)
	  @RequestMapping("/customer")
	  public Object customer(HttpServletRequest request, Model model) {
	      return index(request,model);
	  }
//	  @Permission(validate=false)
//	  @RequestMapping("/index")
	  public Object index(HttpServletRequest request, Model model) {
		  model.addAttribute("actCenterCompanyMap", actCenterCompanyService.getAllCompany());
	      model.addAttribute("typeList", SupplierCustomerType.ALL);
	      model.addAttribute("type", SupplierCustomerType.CUSTOMER_CODE);
	      model.addAttribute("catagoryList", SupplierCustomerCatagoryType.ALL);
//	      return "com/supplier_customer";
	      return BASEPATH+"/index";
	  }
	  
	  /**
       * 获取所有的供应商信息
       * @param request
       * @param response
       * @param page
       * @param rows
       * @return
	 * @throws ParseException 
       */
	  	@ResponseBody
	    @RequestMapping("/data")
	    public LayTableResponse data(HttpServletRequest request, SupplierCustomerQueryParam param) throws ParseException {
	  		logger.debug("客户类型:type={}",param.getType());
	        if(param.isFirst()) {
	        	return  LayTableResponse.nullDataPage();
	        }else {
	        	return customerSupplierService.selectData(param);
	        }
	    }
	  	/**
	  	 * 供应商客户新增
	  	 * @param companyId
	  	 * @param newType
	  	 * @return
	  	 */
	  	@Permission(validate=false)
	  	@RequestMapping("/addinit")
	  	public ModelAndView toAdd(Integer companyId,String newType) {
	  		ModelAndView mv = new ModelAndView();
	  		logger.debug("newType={}"+newType);
	  		//公司
	  		mv.addObject("actCenterCompanyMap", actCenterCompanyService.getAllCompany());
//	  		//部门
//	  		mv.addObject("dept", departmentService.getByCompanyId(companyId));
	  		//分类
	  		mv.addObject("catagoryList", SupplierCustomerCatagoryType.ALL);
	  		if(newType.equals(SupplierCustomerType.SUPPLIER_CODE)) {
	  		mv.addObject("type", SupplierCustomerType.SUPPLIER);
	  		}else{
	  		mv.addObject("type", SupplierCustomerType.CUSTOMER);
	  		}
	  		mv.setViewName(BASEPATH+"/add");
	  		return mv;
	  	}
	  	
	  	/**
	  	 * 新增保存
	  	 * @param request
	  	 * @param supplierCustomer
	  	 * @return
	  	 */
		@SysLog(description="企业管理客户新增保存")
	  	 @ResponseBody
	     @RequestMapping("/add")
	     public Object add(HttpServletRequest request, SupplierCustomer supplierCustomer) {
//	         comSupplierCustomer.setType(Integer.parseInt(sessionService.getByKey("comSupplierCustomerType",request).toString()));
//	         comSupplierCustomer.setCompany_id(SessionUtil.getLoggedCompanyId(request, comSupplierCustomer.getCompany_id()));
	  		 logger.debug("供应商管理信息修改:id={}",supplierCustomer.getId());
	        return customerSupplierService.edit(supplierCustomer);
//	  		 return new Response<String>(ResultEnum.SUCCESS);
	     }
	  	
	  	
	  	/**
	  	 * 客户供应商编辑
	  	 * @param request
	  	 * @param id
	  	 * @return
	  	 */
	  	@Permission(validate=false)
	     @RequestMapping("/edit")
	     public ModelAndView edit(@RequestParam("id") Integer id) {
	    	 ModelAndView mv = new ModelAndView();
	    	SupplierCustomer customer =  customerSupplierService.selectById(id);
	    	logger.debug("type={}",customer.getType());
	    	if(customer.getType().toString().equals(SupplierCustomerType.SUPPLIER_CODE)) {
		  		mv.addObject("type", SupplierCustomerType.SUPPLIER);
		  		logger.debug("SUPPLIER={}",customer.getType());
	  		}else{
	  		mv.addObject("type", SupplierCustomerType.CUSTOMER);
	  		logger.debug("CUSTOMER={}",customer.getType());
	  		}
	    	//公司
	  		 mv.addObject("actCenterCompanyMap", actCenterCompanyService.getAllCompany());
	    	 mv.addObject("catagoryList", SupplierCustomerCatagoryType.ALL);
	    	 mv.addObject("dept",departmentService.getByCompanyId(customer.getCompanyId()));
	    	 mv.addObject("staff", staffService.getStaffList(customer.getDepartmentId(), customer.getCompanyId()));
	    	 mv.addObject("obj", customer);
	    	 mv.setViewName(BASEPATH+"/edit");
	    	 return mv;
	    	 
	     }
	  	/**
	  	 * 修改保存
	  	 * @param request
	  	 * @param supplierCustomer
	  	 * @return
	  	 */
	  	@SysLog(description="企业管理客户修改保存")
	  	 @ResponseBody
	     @RequestMapping("/edit_execute")
	     public Object editExecute(HttpServletRequest request, SupplierCustomer supplierCustomer) {
//	         comSupplierCustomer.setType(Integer.parseInt(sessionService.getByKey("comSupplierCustomerType",request).toString()));
//	         comSupplierCustomer.setCompany_id(SessionUtil.getLoggedCompanyId(request, comSupplierCustomer.getCompany_id()));
	  		 logger.debug("供应商管理信息修改:id={}",supplierCustomer.getId());
	        return customerSupplierService.edit(supplierCustomer);
//	  		 return new Response<String>(ResultEnum.SUCCESS);
	     }
	  	 
	  	/**
	      * 删除
	      * @param request
	      * @param id
	      * @return
	      */
	  	 @SysLog(description="企业管理客户删除")
	     @ResponseBody
	     @RequestMapping("/del_execute")
	     public Object delExecute(HttpServletRequest request, @RequestParam("id") Integer id) {
	    	logger.debug("供应商管理删除操作:id={}",id);
//	    	 return new Response<String>(ResultEnum.SUCCESS);
	    	 return customerSupplierService.del(id);
	     }
	     
	     /**
	      * 供应商客户详情
	      */
	     @Permission(validate=false)
	     @RequestMapping("/view")
	     public ModelAndView view(Integer id) {
	    	 ModelAndView mv = new ModelAndView();
	    	 mv.addObject("obj", customerSupplierService.selectDetailById(id));
	    	 mv.setViewName(BASEPATH+"/view");
	    	 return mv;
	     }
	     /**
	      * 根据公司id获取部门
	      * @param companyId
	      * @return
	      */
	    @Permission(validate=false)
	    @RequestMapping("/getDeptById")
	    @ResponseBody
	    public List<Department> getDeptList(Integer companyId) {
	    	return  departmentService.getByCompanyId(companyId);
	    }
	    /**
	     * 根据公司id，部门id查询员工
	     * @param deptId
	     * @param companyId
	     * @return
	     */
	    @Permission(validate=false)
	    @RequestMapping("/getStaffById")
	    @ResponseBody
	    public List<StaffInfo> getStaffList(Integer deptId,Integer companyId){
	    	return staffService.getStaffList(deptId, companyId);
	    }
}
